package com.fsoft.finaltest.dao;

import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import com.fsoft.finaltest.dto.Animal;

public class AnimalJDBCTemplate implements AnimalDAO{
	private DataSource dataSource;
	private JdbcTemplate jdbcTemplateObject;
	
	@Override
	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	    this.jdbcTemplateObject = new JdbcTemplate(this.dataSource);
	}

	@Override
	public void create(Integer id, String animalName, boolean state) {
		
		String SQL = "INSERT INTO Animal(AnimalID, AnimalName, State) VALUES (?,?,?)";
	    jdbcTemplateObject.update( SQL, id, animalName, state);
	    return;
	}

	@Override
	public Animal getAnimal(Integer id) {
		String SQL = "select * from Animal where AnimalID = ?";
	    Animal animal = jdbcTemplateObject.queryForObject(SQL, 
	                        new Object[]{id}, new AnimalMapper());
	    return animal;
	}

	@Override
	public List<Animal> listAnimal() {
		String SQL = "select * from Animal";
	    List <Animal> animals = jdbcTemplateObject.query(SQL, 
	                                new AnimalMapper());
	    return animals;	
	}

	@Override
	public void delete(Integer id) {
		String SQL = "delete from Animal where AnimalID = ?";
	    jdbcTemplateObject.update(SQL, id);
	    return;
	}

	@Override
	public void update(Integer id, String animalName, boolean state) {
		String SQL = "update Animal set AnimalName = ? and State = ? where AnimalID = ?";
	    jdbcTemplateObject.update(SQL, animalName, state, id);
	    return;
	}
	
	
}
